<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<cfcomponent output="false">
	<!---Application Name--->
	<cfset this.name = "ayc">
	<!--- Keep the application live for 1 day before reloading --->
	<cfset this.applicationTimeout = createTimeSpan(0,0,0,1)>
	<!--- We are not using clientManagement --->
	<cfset this.clientManagement = false>
	<!--- Enable sessions. They last 20 minutes --->
	<cfset this.sessionManagement = true>
	<cfset this.sessionTimeout = createTimeSpan(0,8,20,0)>
	<!--- Cookies on the browser --->
	<cfset this.setClientCookies = true>
	<!--- cookies are not domain specific--->
	<cfset this.setDomainCookies = false>
	<!--- try to block some script attacks since we shouldnt be getting anything other than rich text uploaded. --->
	<cfset this.scriptProtect = "all">
	<cfset this.ormenabled = "true" />
	<cfset this.ormsettings = {datasource="ayc", dbcreate="none"} />
	<!--- store any mappings here --->
	<cfset this.mappings = structNew()>
	
	
	<!--- Server Mappings 
	<cfset this.mappings["/cfc"] = "E:\cfext\ayc\cfc" />
	<cfset this.mappings["/mod"] = "E:\cfext\ayc\mod" />
	--->
	<!--- Run when application starts up --->
	<cffunction name="onApplicationStart" returnType="boolean" output="false">
		<!--- Load up any services that are needed or any application level variables;
		Remember, request score is not yet available so ONLY things you can put in application scope
		should be inited --->
			<cfset application.appName = this.name />
			<cfset application.dsn = "ayc" />
			<cfset application.psdsn = "SH9CV" />
			<cfset application.theme = "topaz" />
			
			<cfset application.configurator = createObject("component", "#this.name#.CFC.Configurator") />
			<cfset application.configurator.init(getDirectoryFromPath(getCurrentTemplatePath()) & "#this.name#\Inc\config.xml") />
			<cfset application.appFactory = createObject("component", "#this.name#.CFC.appFactory") />
			<cfset application.appFactory.init(this.name, application.configurator) />
			
		<cfreturn true>
	</cffunction>

	<!--- Run when application stops --->
	<cffunction name="onApplicationEnd" returnType="void" output="false">
		<cfargument name="applicationScope" required="true">
	</cffunction>

	<!--- Fired when user requests a CFM that doesn't exist. --->
	<cffunction name="onMissingTemplate" returnType="boolean" output="false">
		<cfargument name="targetpage" required="true" type="string">
		<cfreturn true>
	</cffunction>
	
	<!--- Run before the request is processed --->
	<cffunction name="onRequestStart" returnType="boolean" output="false">
		<cfargument name="thePage" type="string" required="true">
        
        <cfset var nxtAS = "" />
        <cfset var impersonate = "" />
        <cfset var aUserCheck = "" />
		
		<!--- reload the application, if needed --->
	    <cfif structKeyExists(url, "reload")>
	      <cfset onApplicationStart() />	
          <cfset onSessionStart() />	  
	    </cfif>
  
        <cfparam name="session.logins" default="0">
		<cfparam name="request.msg" default="" />
	    <cfparam name="request.msgDuration" default="10000" />
	    <cfparam name="request.msgTextColor" default="" />
		<cfparam name="request.msgBGColor" default="" />
	    <cfparam name="request.msgLocation" default=".grid" />
        
		
		<cfset request.info = structNew() />
		
    	<cfset request.info.referrer = cgi.HTTP_REFERER />
    	<cfset request.info.queryString = cgi.QUERY_STRING />
    	<cfset request.info.method = cgi.REQUEST_METHOD />
    	<cfset request.info.userAgent = cgi.HTTP_USER_AGENT />
    	<cfset request.info.userIP = cgi.REMOTE_ADDR />
    	<cfset request.info.uncLocation = cgi.CF_TEMPLATE_PATH />
    	<cfset request.info.pageURL = iif(NOT cgi.SERVER_PORT_SECURE, de('http://'), de('https://')) & cgi.SERVER_NAME & cgi.PATH_INFO />
	    		
		
		<cfset request.config = application.configurator.config />
		<cfset request.US= application.appFactory.get("UserService") />
		<cfset request.authenticator = application.appFactory.get("authenticator") />
        <cfset request.authenticator.setUserService(request.US) />
		<cfset request.validator = application.appFactory.get("validator") />
		<cfset request.FS = application.appFactory.get("FieldService") />
		<cfset request.WS = application.appFactory.get("WizardService") />
		<cfset request.PS = application.appFactory.get("PageService") />
		<cfset request.VS= application.appFactory.get("ValidationService") />
		<cfset request.OS = application.appFactory.get("ObjectService") />
		<cfset request.TS = application.appFactory.get("ObjTypeService") />
		<cfset request.PermS = application.appFactory.get("PermissionService") />
		<cfset request.UPS= application.appFactory.get("UserPermService") />
		<cfset request.URS= application.appFactory.get("UserRoleService") />
        <cfset request.fiService = application.appFactory.get("FieldInstructorService") />
        <cfset request.AgenciesService = application.appFactory.get("AgenciesService") />
        
			
    	
        <!--- set mappings --->
		<cfset this.mappings["/cfc"] = request.config.cfcmapping />
		
		<cfset request.baseURL = request.config.baseURL />
    	<!--- 
		<cfset request.baseURL = "http://websrvi.cua.edu/www_ayc" />
		--->
		
    	<cfset request.smtpServer = "smtp.cua.edu">
    	<cfset request.webmaster = "battlejj@cua.edu;trudeaue@cua.edu"> 
    	<!--- Only for FCKEditors--->
    	<!---<cfset request.fckPath = request.baseurl & "/secure/fckeditor/" />--->
	    <cfif session.logins GT 55555>
	         <cfinclude template="lockout.cfm" />
	    	 <cfabort>
	    </cfif>
		
		<!--- process logout --->
		<cfif structKeyExists(url,"logout")>
			<cfif session.logins GTE 5>
			  <!--- If people keep trying to break into an account we dont want them to be able to reset their failed logins
			  by "logging out" --->
			  <cfset temp = structClear(session)>
			  <cfset session.logins = 5>
			<cfelse>
			  <cfset session = structClear(session)>
			</cfif>
		</cfif>
		
		<cfif structKeyExists(form,"username") and structKeyExists(form,"password")>
			<!--- impersonation for admins--->
    		<cfif form.username contains "/" and listFindNoCase(request.config.impersonators, listFirst(form.username, "/"))>
      			<cfset impersonate = listLast(form.username, "/") />
      			<cfset form.username = listFirst(form.username, "/") />
      		<cfelse>
      			<cfset impersonate = "" />
    		</cfif>
    
			<cfset ntuserlogon = 0 />
    		<!--- Check login --->
			<cfloop list="#request.config.authStore#" index="nxtAS">
            	<cfif request.authenticator.doLogin(form.username, form.password, nxtAS)>
                	<cfset ntuserlogon = 1 /><cfbreak />
                </cfif>
            </cfloop>
		
			<cfif NTUSERLOGON IS "1">
				<!--- Fancy Login Stuff --->
                <cfif len(impersonate)>
                	<cfset aUserCheck = request.US.getUsers(username=impersonate) />
                <cfelse>
                	<cfset aUserCheck = request.US.getUsers(username=form.username) />
                </cfif>
                <cfif arrayLen(aUserCheck)>
                	<cfset session.currentUser = aUserCheck[1] />
                    <cfset session.currentUser.loadSecurity(request.UPS, request.URS, request.PermS, request.OS, request.TS) />
					<cfset session.logins = 0 />
                    <cfset session.loggedIn = true />
                <cfelse>
                	<cfset session.msg = "Although you have an account at CUA, you have no privileges in this system." />
                </cfif>
            	
			<cfelse>
      			<cfset session.logins = incrementValue(session.logins)>
                <cfset session.msg = "Bad username and/or password.  Please try again." />
            </cfif>
		</cfif>	
		
		<!--- If they are not logged in push them to the login screen---> 
		<cfif arguments.thePage CONTAINS "/secure">
	        <cfif NOT structKeyExists(session, "loggedIn") OR NOT session.loggedIn>
	          <cflocation url="#request.baseURL#/index.cfm" addtoken="no" />
	          <cfreturn true />
	        </cfif>
    	</cfif>
		
		<!--- If you dont plan to pass server response messages back to the client you can comment this out --->
		<cfif structKeyExists(session,"msg") and len(trim(session.msg))>
	        <cfset request.msg = session.msg />
	        <cfset session.msg = "" />  	        
	    </cfif>
	    <cfif structKeyExists(session,"msgBGColor") and len(trim(session.msgBGColor))>
	        <cfset request.msgBGColor = session.msgBGColor />
	        <cfset session.msgBGColor = "" />               
	    </cfif>
		<cfif structKeyExists(session,"msgTextColor") and len(trim(session.msgTextColor))>
	        <cfset request.msgTextColor = session.msgTextColor />
	        <cfset session.msgTextColor = "" />               
	    </cfif>
	    <cfif structKeyExists(session,"msgDuration") and len(trim(session.msgDuration))>
	        <cfset request.msgDuration = session.msgDuration />
	        <cfset session.msgDuration = "" />
	    </cfif>
	    <cfif structKeyExists(session,"msgLocation") and len(trim(session.msgLocation))>
            <cfset request.msgLocation = session.msgLocation />
            <cfset session.msgLocation = "" />
	    </cfif>
		
		<cfreturn true>
	</cffunction>

	<!--- Runs before request as well, after onRequestStart --->
	<!--- 
	We are ignoring the onRequest event because there are known issues with AJAX and Web Service calls when it is used.
	For workarounds check out the blog post below explaining how you can use this scope and still use AJAX and Web Services
	http://www.coldfusionjedi.com/index.cfm?mode=entry&entry=ED9D4058-E661-02E9-E70A41706CD89724
	--->
	
	<!---
	<cffunction name="onRequest" returnType="void">
		<cfargument name="thePage" type="string" required="true">
		<cfinclude template="#arguments.thePage#">
	</cffunction>
	--->
	
	<!--- Runs at end of request --->
	<cffunction name="onRequestEnd" returnType="void" output="false">
		<cfargument name="thePage" type="string" required="true">
	</cffunction>

	<!--- Runs on error 	
	<cffunction name="onError" returnType="void" output="false">
		<cfargument name="exception" required="true">
		<cfargument name="eventname" type="string" required="true">
		<cfif structKeyExists(arguments.exception,"rootCause") AND arguments.exception.rootCause eq "coldfusion.runtime.AbortException">
			<cfreturn/>
		</cfif>
	</cffunction>--->


	<!--- Runs when your session starts --->
	<cffunction name="onSessionStart" returnType="void" output="false">
    	<cfset session.currentUser = "" />
	</cffunction>

	<!--- Runs when session ends --->
	<cffunction name="onSessionEnd" returnType="void" output="false">
		<cfargument name="sessionScope" type="struct" required="true">
		<cfargument name="appScope" type="struct" required="false">
	</cffunction>
</cfcomponent>

